<?php

namespace App\Http\Controllers\Client\Common;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class HistoryLotteryController extends ClientController
{
    /**
     * 查询霸王餐活动记录
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 10;
        $pageNo = $all['pageNo'] ?? 1;

        try {
            $where = function($query) use ($all){
                $query->where('la.creator', '=', $this->we_id);
                if($all['shop_id'] > 0){
                    $query->where('la.shop_id','=',$all['shop_id']);
                }
                if($all['goods_id'] > 0){
                    $query->where('la.goods_id','=',$all['goods_id']);
                }
                if($all['status'] !== -1){
                    $query->where('la.status','=',$all['status']);
                }
                if($all['code']){
                    $code = $all['code'];
                    $query->whereRaw('(lud.code LIKE "'.$code.'%" OR talent.account_name LIKE "%'.$code.'%")');
                }
            };
            $result = DB::table('lottery_activity as la')
                ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
                ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
                ->leftJoin('lottery_draw_detail as ldd', 'la.act_id', '=', 'ldd.act_id')
                ->leftJoin('talent', 'ldd.we_id', '=', 'talent.we_id')
                ->leftJoin('explore_channel as ec', 'talent.channel_id', '=', 'ec.channel_id')
                ->leftJoin('lottery_users_draw as lud', 'ldd.participate_id', '=', 'lud.participate_id')
                ->leftJoin('lottery_users as lu','ldd.we_id','=','lu.we_id')
                ->where($where)
                ->orderByDesc('la.created_at')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->select('la.act_id', 'la.goods_id', 'la.shop_id', 'la.start_time','la.draw_time', 'la.status', 'la.is_stop','la.canceled_at','la.remark',
                    'shop.shop_type', 'shop.shop_name', 'lg.goods_name','lg.price', 'ldd.participate_id', 'ldd.consumed', 'ldd.consumed_at',
                    'lud.code', 'talent.channel_id', 'talent.verify_img','talent.collection_qr_code', 'ec.channel_name', 'talent.account_name','lu.phone')
                ->get()
                ->map(function ($item) {
                    $item->status_name = Data::$lotteryActivityStatus[$item->status];
                    $item->shop_type_name = Data::$laifeiShopTypes[$item->shop_type];
                    return $item;
                })
                ->toArray();
            $result = json_decode(json_encode($result), true);

//            // 参与人数
//            $act_ids = Arr::pluck($result, 'act_id');
//            $counts = DB::table('lottery_users_draw')
//                ->whereIn('act_id', $act_ids)
//                ->groupBy('act_id')
//                ->select('act_id')
//                ->selectRaw('COUNT(*) as count')
//                ->get()
//                ->mapWithKeys(function ($item) {
//                    return [$item->act_id => $item->count];
//                })
//                ->toArray();
//            $counts = json_decode(json_encode($counts), true);
//
//            foreach ($result as $key => &$value) {
//                $value['count'] = $counts[$value['act_id']] ?? 0;
//            }

        } catch (\Exception $e) {
            Log::error(HistoryLotteryController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '信息查询失败');
        }

        return $this->setReturnJson(0, 'success', $result);

    }

}
